{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "zBubcXOfW3Au"
      },
      "source": [
        "##### *Copyright 2020 Google LLC*\n",
        "*Licensed under the Apache License, Version 2.0 (the \"License\")*"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "cellView": "both",
        "colab": {},
        "colab_type": "code",
        "id": "rKwqeqWBXANA"
      },
      "outputs": [],
      "source": [
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "# https://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "dGib_ljiVqZp"
      },
      "source": [
        "# Build the C++ \"edgetpu\" examples for Coral\n",
        "\n",
        "This Colab provides a convenient way to build the C++ \"edgetpu\" inferencing examples: [github.com/google-coral/edgetpu/tree/master/src/cpp/examples](https://github.com/google-coral/edgetpu/tree/master/src/cpp/examples).\n",
        "\n",
        "Simply run this notebook and it produces the downloadable binaries for your target system (default target is `aarch64`, which is compatible with the Coral Dev Board).\n",
        "\n",
        "To start the build,  select **Runtime \u003e Run all** in the Colab toolbar.\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "viewin-badges"
      },
      "source": [
        "\u003ca href=\"https://colab.research.google.com/github/google-coral/tutorials/blob/master/build_cpp_examples.ipynb\" target=\"_parent\"\u003e\u003cimg src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\"\u003e\u003c/a\u003e\n",
        "\u0026nbsp;\u0026nbsp;\u0026nbsp;\u0026nbsp;\n",
        "\u003ca href=\"https://github.com/google-coral/tutorials/blob/master/build_cpp_examples.ipynb\" target=\"_parent\"\u003e\u003cimg src=\"https://img.shields.io/static/v1?logo=GitHub\u0026label=\u0026color=333333\u0026style=flat\u0026message=View%20on%20GitHub\" alt=\"View in GitHub\"\u003e\u003c/a\u003e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "fmb_p_OkCp0J"
      },
      "source": [
        "## Install Bazel"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "B6KvyX7hFdVB"
      },
      "outputs": [],
      "source": [
        "! sudo apt install curl\n",
        "! curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -\n",
        "! echo \"deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8\" | sudo tee /etc/apt/sources.list.d/bazel.list"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "w9YmfWd0Fnjj"
      },
      "outputs": [],
      "source": [
        "! sudo apt update \u0026\u0026 sudo apt install bazel"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "0H4Bv2yWFyMZ"
      },
      "source": [
        "## Download examples from GitHub"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "2ZbLwr4bF655"
      },
      "outputs": [],
      "source": [
        "! git clone https://github.com/google-coral/edgetpu.git"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "y_kKEIB-F8n_"
      },
      "source": [
        "## Build all examples for Dev Board"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "WMiVEbZReILd"
      },
      "outputs": [],
      "source": [
        "! sudo apt-get install -y crossbuild-essential-armhf crossbuild-essential-arm64 bazel"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "b5-XBtOHTKUq"
      },
      "outputs": [],
      "source": [
        "%cd edgetpu"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "adMIBqbAS0zb"
      },
      "source": [
        "The following line builds for an ARM64 system (`aarch64`). Alternative CPU architectures are `k8` and `armv7a`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "lG1HxB21BA8d"
      },
      "outputs": [],
      "source": [
        "! make CPU=aarch64 examples"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "-8zj1nAPpeQg"
      },
      "source": [
        "## Download the binaries"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 0,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "SU79MMc5rD0I"
      },
      "outputs": [],
      "source": [
        "from google.colab import files\n",
        "\n",
        "files.download('bazel-out/aarch64-opt/bin/src/cpp/examples/classify_image')\n",
        "files.download('bazel-out/aarch64-opt/bin/src/cpp/examples/two_models_one_tpu')\n",
        "files.download('bazel-out/aarch64-opt/bin/src/cpp/examples/two_models_two_tpus_threaded')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "Y_YDWKRvx8v-"
      },
      "source": [
        "The downloads might fail if you used **Run all**. If so, just retry by clicking **Run cell** in this last code cell.\n",
        "\n",
        "**Note:** You probably need to modify the file permissions in order to run it."
      ]
    }
  ],
  "metadata": {
    "colab": {
      "collapsed_sections": [
        "zBubcXOfW3Au"
      ],
      "name": "build_cpp_examples.ipynb",
      "provenance": [],
      "toc_visible": true
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
